import Vue from 'vue'
import VueRouter from 'vue-router'
// 引入自己的组件
import Login from '@/components/login'
import Home from '@/components/home'
import Welcome from '@/components/welcome'
import Users from '@/components/user/users'
import Rights from '@/components/power/rights'
import Roles from '@/components/power/roles'
import List from '@/components/goods/list'
import Add from '@/components/goods/add'
import Edit from '@/components/goods/edit'
import Cate from '@/components/goods/cate'
import Params from '@/components/goods/params'
import Order from '@/components/order/orders'
import Reports from '@/components/report/reports'


Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  { path: '/home',redirect:'/welcome', component: Home,
    children:[
      {path:'/welcome',component:Welcome},
      {path:'/users',component:Users},
      {path:'/rights',component:Rights},
      {path:'/roles',component:Roles},
      {path:'/goods',component:List},
      {path:'/goods/add',component:Add},
      {path:'/goods/edit',component:Edit,name:'edit'},
      {path:'/categories',component:Cate},
      {path:'/params',component:Params},
      {path:'/orders',component:Order},
      {path:'/reports',component:Reports},
    ]
  },
]

const router = new VueRouter({
  routes
})
// 配置路由导航守卫（路由拦截器）
router.beforeEach((to,from,next)=>{
  // to:将要访问的路由；from:从哪个路由跳转而来；next:函数，next()放行 next('/login')跳转
  if(to.path=='/login') return next();
  // 获取token
  let token = window.sessionStorage.getItem('token')
  if(token) {
    next()
  }
  else{
    return next('/login')
  }
})

export default router
